﻿#ifndef BASEOBJECT_H
#define BASEOBJECT_H


#include "../_Global/GlobalClass.h"
#include "GameDefine.h"
//#include "_Global/UIGlobal.h"

#include <QObject>


///////////////////////////////////////////////////////////////////

//所有物件的基类
class _BaseObject : public QObject
{
    Q_OBJECT
    Q_FLAGS(_Flags)
    Q_FLAGS(_Areas)
    Q_ENUMS(_ObjType)
    Q_ENUMS(_Area)
    //Q_ENUMS(Flag)

    Q_PROPERTY(qint32 id READ id WRITE setId)
    Q_PROPERTY(_ObjType objType READ objType)
    Q_PROPERTY(_Flags flags READ flags WRITE setFlags NOTIFY flagsChanged)
public:

    //属性数据:对象类型
    enum _ObjType {
        ObjType_NULL = 0,                  //无
        ObjType_Pet = 1,                   //
        ObjType_JiNeng = 2,                //技能
        ObjType_Player = 4,                //玩家
        ObjType_Button = 128,              //按钮
    };

    //游戏中使用:对象状态
    enum _Flag {
        Flag_Null = 0,          //无
        Flag_Enable = 1,        //亮色
        Flag_CanClicked = 2,    //可点
        Flag_Clicked = 4,       //已点

        //flag_ZhengMian = 4,     //正面
        //flag_Alpha = 8,        //半透明
        //flag_Show = 1,          //显示
    };
    Q_DECLARE_FLAGS(_Flags, _Flag)        //写在和E1同命名空间下(或同一类中)

    //设置状态时参数类型
    enum _FlagType {
        FlagType_Set,   //直接设置
        FlagType_Add,   //增加一或多
        FlagType_Clear, //清除一或多
    };


    //所有可 点击/放置 区域
    enum _Area {
        Area_Null = 0,
        /*Area_PaiDui = 1,   //牌堆
        Area_QiPaiDui = 2, //弃牌堆
        Area_ShouPai = 4,   //手牌区
        Area_PanDing = 8,   //判定区

        Area_WuQi = 16, //装备区
        Area_FangJu = 32, //装备区
        Area_JiaMa = 64, //装备区
        Area_JianMa = 128, //装备区
        Area_ZhuangBei = Area_WuQi | Area_FangJu | Area_JiaMa | Area_JianMa, //装备区

        Area_JiNeng = 256,   //技能

        Area_WuJiang = 512,   //武将区

        Area_Player = 1024,   //玩家区

        Area_Button = 2048,   //按钮*/
    };
    Q_DECLARE_FLAGS(_Areas, _Area)        //写在和E1同命名空间下(或同一类中)



public:
    //int changdu;    //长度
    explicit _BaseObject(QObject *parent = nullptr): QObject(parent)
    {
        setProp(-1, ObjType_NULL, Area_Null);
        init();
    }

    explicit _BaseObject(const _BaseObject& obj)
    {
        //*this = obj;
    }

    virtual ~_BaseObject()
    {
    }


signals:
    void flagsChanged(_Flags flags);

public:
    virtual void init()
    {
        clearSomeFlags(Flag_Null);
        m_bUsing = false;
    }

    //设置牌的类型和值
    Q_INVOKABLE void setProp(qint32 id = -1, _ObjType type = ObjType_NULL, _Area area = Area_Null)
    {
        m_nID = id;
        m_eObjType = type;
        m_eArea = area;
    }



    _BaseObject& operator=(_BaseObject& _t)
    {
        this->m_nID = _t.m_nID;
        this->m_eObjType = _t.m_eObjType;
        //this->eValue = _t.eValue;
        this->setFlags(_t.m_eFlags);
        this->m_eArea = _t.m_eArea;

        return *this;
    }




//property
    qint32 id() const
    {
        return m_nID;
    }

    void setId(qint32 &id)
    {
        m_nID = id;
    }


    _Flags flags() const
    {
        return m_eFlags;
    }

    void setFlags(_Flags flags,_FlagType type)
    {
        switch (type)
        {
        case FlagType_Set:
            setFlags(flags);
            break;
        case FlagType_Add:
            setSomeFlags(flags);
            break;
        case FlagType_Clear:
            clearSomeFlags(flags);
            break;
        default:
            break;
        }
    }

    void setFlags(_Flags flags)
    {
        if(m_eFlags != flags) {
            m_eFlags = flags;
            emit flagsChanged(m_eFlags);
            //qDebug()<<"flagsChanged send"<<m_eObjType<<m_nID<<m_eFlags;
        }
        else
        {
            //emit flagsChanged(m_eFlags);
            //qDebug()<<"flagsChanged send same"<<m_eObjType<<m_nID<<m_eFlags;
        }
    }

    void setSomeFlags(_Flags flags)
    {
        if((m_eFlags & flags) != flags)
        {
            m_eFlags |= flags;
            emit flagsChanged(m_eFlags);
            //qDebug()<<"flagsChanged send";
        }
        else
        {
            //qDebug()<<"flagAChanged send same"<<m_eObjType<<m_nID<<m_eFlags<<flags;
        }
    }
    void clearSomeFlags(_Flags flags)
    {
        if((m_eFlags & flags) != 0) {
            m_eFlags ^= flags;
            emit flagsChanged(m_eFlags);
            //qDebug()<<"flagsChanged send";
        }

        //m_eFlags &= ~flags;
    }

    _ObjType objType() const
    {
        return m_eObjType;
    }




public:
    bool        m_bUsing;       //是否使用

protected:
    qint32      m_nID;          //ID(下标),一个类型中唯一

    _ObjType    m_eObjType;     //类型
    _Flags      m_eFlags;       //状态
    _Area       m_eArea;        //区域


};


Q_DECLARE_METATYPE(_BaseObject*)
//Q_DECLARE_METATYPE(_Pet::_Flags)
Q_DECLARE_OPERATORS_FOR_FLAGS(_BaseObject::_Flags)  //写在全局
Q_DECLARE_OPERATORS_FOR_FLAGS(_BaseObject::_Areas)  //写在全局

#endif // BASEOBJECT_H
